package vek.GUI.Infoview;

import java.awt.event.ActionEvent;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import vek.GUI.MainWindow;

/**
 * This class contains the JComponents which makes up the InfoView.
 * JComponents: 
 * <p>
 * WebsiteViewer (Shows the website connected to the POI).
 * <br>
 * InfoDescriptionViewer (Shows the description conntected to the POI).
 * 
 * @author Christian, Michal og Jakob (Hold1, gruppe 2)
 */
public class InfoView extends JScrollPane
{
        private Dimension screenSize;
        private boolean isVisible;
        private String url;
        private String description;
        
        private JButton closeButton;
        private JPanel mainView;
        private WebsiteViewer websiteviewer;
        private InfoDescriptionShower descriptionShower;
        private MainWindow mainWindow;
        
    /**
     * 
     * @param size the size of this component
     * @param scrSize the screensize of the termianl
     * @param mv the mainWindow which this JComponent contains
     */
        
    public InfoView(Dimension size, Dimension scrSize, MainWindow mv)
    {
        screenSize = scrSize;
        mainWindow = mv;
        
        websiteviewer = new WebsiteViewer(null);
        descriptionShower = new InfoDescriptionShower("default");
        mainView = new JPanel();
        closeButton = new JButton("X");
        closeButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                closeInfo();
            }
        });
        
        description = "";
        
        
        setPreferredSize(screenSize);
        setVisible(false);
        
        createLayout();
        addContents();
        
        
        
    }
    
    /**
     * Adds the JComponents to the mainView and then adds mainView to this JComponent.
     */
    
    private void addContents()
    {   
        mainView.add(closeButton);
        mainView.add(descriptionShower);
        mainView.add(websiteviewer);
        getViewport().add(mainView);
    }
    
    /**
     * Creates the layout for this JComponent, the layout used is BoxLayout
     */
    
    private void createLayout()
    {
        mainView.setLayout(new BoxLayout(mainView, BoxLayout.Y_AXIS));
    }
    
    /**
     * Makes this JComponent visible and shows the description and website
     * @param d the description of the POI
     * @param url the URL to the website which is connected to the POI
     */
    
    public void showInfo(String d, String url)
    {
        descriptionShower.setDescription(d);
        websiteviewer.setPageURL(url);
        setVisible(true);
        mainWindow.paintComponents(mainWindow.getGraphics()); // draws the components again to make sure that the changes made are reflected on the screen.
        // if this is not done then the screen will not be refreshed and JComponents won't be visble untill the JFrame is resized/a redraw is called
        
    }
    
    /**
     * Hides this JComponent
     */
    
    
    public void closeInfo()
    {
        websiteviewer.setContentType("text/plain"); // makes sure that website is "unloaded" so that if an error occurs when next POI is loaded the old website won't be shown
        setVisible(false);
        mainWindow.paintComponents(mainWindow.getGraphics()); // redraws components, see showInfo()
    }
    
    

}
